package com.atguigu.gmall.product.utils;

import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;

/**
 * @author 李旭
 * @date 2022/1/4 11:18
 * @Description:
 *   上传文件 工具类
 *
 *   #连接超时时间  电话未接通  毫秒
 * connect_timeout = 30000
 * #网络超时时间  电话的通话时间
 * network_timeout = 60000
 * #字符集
 * charset = UTF-8
 *
 *    在Nacos配置中心中
 *       项目启动之后： bootstrap.yml  配置Nacos配置中心的地址
 *              config:
 *         server-addr: 192.168.200.133:8848
 *         file-extension: yml
 *         group: java210712
 *         namespace: 3c9f05cb-af51-40ed-94c5-4f58ebb49173
 *         ext-config:
 *           - dataId: fdfs_client.conf.properties
 *             group: java20220104
 *             refresh: true
 *      项目中的哪里了？  SpringIOC容器中
 *
 * #
 * tracker_server = 192.168.200.133:22122
 * #tracker_server = 192.168.200.134:22122
 *
 */
@Component
public class FileUploadUtils {//new  FileUploadUtils
    //2:构造器
    //3:static
    //4:注入
    //5：SpringIOC容器 后置处理器
    private TrackerClient trackerClient = null;
    //常量
    //public static final String FDFSCLIENTCONF = "fdfs_client.conf.properties";
    //
    @Value("${tracker_server}")
    private String FDFSCLIENTCONF;
    @Value("${connect_timeout}")
    private Integer connectTimeout;
    //SpringIOC容器 后置处理器
    @PostConstruct //后置处理器
    public void init(){
       /* String url = ClassUtils.getDefaultClassLoader().
                getResource(FDFSCLIENTCONF).getPath();*/
        //0:加载配置文件  IO流读取配置文件  IO绝对路径 不认识相对路径
        try {
            //ClientGlobal.init(url);
            InetSocketAddress[] tracker_servers = new InetSocketAddress[1];
            String[] parts = FDFSCLIENTCONF.split("\\:", 2);
            InetSocketAddress inetSocketAddress =
                    new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
            tracker_servers[0] = inetSocketAddress;
            TrackerGroup g_tracker_group = new TrackerGroup(tracker_servers);
            ClientGlobal.setG_tracker_group(g_tracker_group);
            //超时时间
            ClientGlobal.setG_connect_timeout(connectTimeout);
            //1:创建跟踪器
            trackerClient  = new TrackerClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //上传文件
    public String upload(String fileName,byte[] file){
        try {
            //2:连接跟踪器
            TrackerServer trackerServer = trackerClient.getConnection();
            //3: 创建存储节点客户端
            StorageServer storageServer = null;//扩展 功能
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            //4:上传文件
            String ext = FilenameUtils.getExtension(fileName);
            return storageClient1.upload_file1(file, ext, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //下载文件
    public byte[] download(String fileId){
        try {
            //2:连接跟踪器
            TrackerServer trackerServer = trackerClient.getConnection();
            //3: 创建存储节点客户端
            StorageServer storageServer = null;//扩展 功能
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            //4:下载
            return  storageClient1.download_file1(fileId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
